package com.power.algorithm;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/18 3:04 下午
 */
public class Solution125 {
    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        String s1 = "race a car";
        Solution125 solution125 = new Solution125();
        solution125.isPalindrome2(s);
    }


    public boolean isPalindrome(String s) {
        char ch[] = s.toCharArray();
        int left = 0, right = s.length() - 1;

        while (left < right) {
            while (left < right && !Character.isLetterOrDigit(ch[left])) {
                left++;
            }
            while (left < right && !Character.isLetterOrDigit(ch[right])) {
                right--;
            }
            if (Character.toLowerCase(ch[left]) != Character.toLowerCase(ch[right])) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public boolean isPalindrome2(String s) {
        char ch[] = s.toCharArray();
        for (int left = 0, right = s.length() - 1; left < right; left++, right--) {

            while (left < right && !Character.isLetterOrDigit(ch[right])) {
                right--;
            }
            while (left < right && !Character.isLetterOrDigit(ch[left])){
                left++;
            }
            if (Character.toLowerCase(ch[left]) != Character.toLowerCase(ch[right])) {
                return false;
            }
        }
        return true;

    }
}
